#include "mainwindow.h"

#include <DMainWindow>
#include <dapplication.h>
#include <dtitlebar.h>
#include <DButtonBox>
#include <QScreen>
#include <dlabel.h>
#include <QGridLayout>


DWIDGET_USE_NAMESPACE

MainWindow::MainWindow(QWidget *parent)
    : DMainWindow(),m_layout(new QStackedLayout),m_controlsPage(new ControlsPage),m_encryptionPage(new Encryptionpage)
{
    initUI();
    initConnections();
}

MainWindow::~MainWindow()
{

}

void MainWindow::initUI()
{
    //设置界面初始显示大小
    setMinimumSize(qApp->primaryScreen()->availableSize() / 7 * 4);

    m_seg = new DSegmentedControl;

    // 为DSegmentedControl中添加选项
    m_seg->addSegmented("MD5/SHA256/DES/RSA");
    m_seg->addSegmented("base64与图文转换");
    // 在标题栏中显示DSegmentedControl控件
    titlebar()->setCustomWidget(m_seg, true);

    m_layout->addWidget(m_encryptionPage);
    m_layout->addWidget(m_controlsPage);

    DTitlebar *titlebar = this->titlebar();

    DButtonBox *buttonBox = new DButtonBox(titlebar);
    buttonBox->setFixedWidth(170);
    buttonBox->setButtonList({new DButtonBoxButton("浅色模式"), new DButtonBoxButton("深色模式")}, true);
    buttonBox->setId(buttonBox->buttonList().at(0), 0);
    buttonBox->setId(buttonBox->buttonList().at(1), 1);

    if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
        buttonBox->buttonList().at(0)->click();
    } else {
        buttonBox->buttonList().at(1)->click();
    }

    connect(buttonBox, &DButtonBox::buttonClicked, this, [buttonBox](QAbstractButton *button) {
        if (buttonBox->id(button) == 0) {
            DGuiApplicationHelper::instance()->setPaletteType(DGuiApplicationHelper::LightType);
        } else {
            DGuiApplicationHelper::instance()->setPaletteType(DGuiApplicationHelper::DarkType);
        }
    });

    //titlebar->setTitle("图片文字base64编码互换");
    titlebar->addWidget(buttonBox);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(m_layout);

    //  mainLayout->addStretch(); //没生效？
    //mainLayout->addWidget(new DSeparatorHorizontal());

    // 设置主窗口内容
    QFrame *content = new QFrame;
    content->setLayout(mainLayout);

    setCentralWidget(content);

}

void MainWindow::initConnections()
{
    connect(m_seg, &DSegmentedControl::currentChanged, this, [this] (int index) {
        m_layout->setCurrentIndex(index);
    });

//    connect(pRecommend, &DPushButton::clicked, this, [] {
//        DDialog dialog("", "这里现实简要出错信息XXXXXXXX", nullptr);
//        dialog.setIcon(DStyle().standardIcon(DStyle::SP_MessageBoxWarning));
//        dialog.addButton("显示详情");
//        dialog.addButton("确定");
//        dialog.exec();
//    });

    qDebug() << "initConnections()";
}
